import api from '@/api';

export default {
  namespace: 'dashboard',
  state: {
    orderProfitLossAnalysis: {
      list: [],
      pagination: {},
    },
    customers: {
      yesterday: {},
      today: {},
      total: {},
    },
    receipt: {
      yesterday: 0,
      today: 0,
      total: 0,
    },
    balances: {
      yesterday: 0,
      today: 0,
      total: 0,
    },
    cargoVolumes: {
      yesterday: {},
      today: {},
      total: {},
    },
  },
  effects: {
    *queryIncome({ payload }, { call }) {
      return yield call(api.dashboard.queryIncome, payload)
    },
    *queryCost({ payload }, { call }) {
      return yield call(api.dashboard.queryCost, payload)
    },
    *fetchOrderProfitLossAnalysis({ payload }, { call, put }) {
      const response = yield call(
        api.dashboard.profitLossAnalysis.queryOrderProfitLossAnalysis,
        payload
      );
      if (response.errorCode === 0) {
        yield put({
          type: 'saveOrderProfitLossAnalysis',
          payload: response,
        });
      }
    },
    *fetchCustomer({ payload }, { call, put }) {
      const response = yield call(api.dashboard.queryCustomer, payload);
      if (response.errorCode === 0) {
        yield put({
          type: 'saveCustomer',
          payload: response.data,
        });
      }
    },
    *fetchReceipt({ payload }, { call, put }) {
      const response = yield call(api.dashboard.queryReceipt, payload);
      if (response.errorCode === 0) {
        yield put({
          type: 'saveReceipt',
          payload: response.data,
        });
      }
    },
    *fetchBalance({ payload }, { call, put }) {
      const response = yield call(api.dashboard.queryBalance, payload);
      if (response.errorCode === 0) {
        yield put({
          type: 'saveBalance',
          payload: response.data,
        });
      }
    },
    *fetchCargoVolume({ payload }, { call, put }) {
      const response = yield call(api.dashboard.queryCargoVolume, payload);
      if (response.errorCode === 0) {
        yield put({
          type: 'saveCargoVolume',
          payload: response.data,
        });
      }
    },
    *fetchAirline({ payload }, { call, put }) {
      return yield call(api.dashboard.queryAirline, payload);
    },
    *updateHotAirline(_, { call }) {
      const response = yield call(api.dashboard.refreshAirline);
      return new Promise(res => {
        if (response.errorCode === 0) {
          res(response);
        }
      });
    },
    *fetchProfileShareTotal({ payload }, { call, put }) {
      return yield call(api.dashboard.profitShare.queryTotal, payload);
    },
    *fetchProfileShareDetail({ payload }, { call, put }) {
      return yield call(api.dashboard.profitShare.queryDetail, payload);
    },
    *fetchProfileShareDetailCount({ payload }, { call, put }) {
      return yield call(api.dashboard.profitShare.queryDetailCount, payload);
    },
  },
  reducers: {
    saveOrderProfitLossAnalysis(state, { payload }) {
      const { data, pagination } = payload;
      return {
        ...state,
        orderProfitLossAnalysis: {
          list: data,
          pagination,
        },
      };
    },
    saveCustomer(state, { payload }) {
      return {
        ...state,
        customers: payload,
      };
    },
    saveReceipt(state, { payload }) {
      return {
        ...state,
        receipt: payload,
      };
    },
    saveBalance(state, { payload }) {
      return {
        ...state,
        balances: payload,
      };
    },
    saveCargoVolume(state, { payload }) {
      return {
        ...state,
        cargoVolumes: payload,
      };
    },
  },
};
